import java.util.Arrays;

/**
 * @Author 1928
 * @Date 2022/4/20 9:16
 * @Description  数组
 */
public class TextWei {
    /**
     * 写一个函数 数组以字符串的形式输出 [1,2,3,4]
     * @param array
     * @return
     */
    public static String myToString(int[] array) {
        String mool ="[";
        for (int i = 0; i < array.length; i++) {
            mool = mool + array[i];
            if (i != array.length-1){
                mool += ",";
            }
        }
        mool+="]";
        return mool;
    }
    public static void main1(String[] args) {
        int[] array = {1,2,3,4};
        String ret = myToString(array);
        System.out.println(ret);
    }

    /**
     * 四种拷贝方式
     * 使用for循环拷贝
     * @param args
     */
    public static void main2(String[] args) {
        int array[] = {1,2,3,4};
        int copy[] = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }
        System.out.println(Arrays.toString(copy));
    }
    public static void main10(String[] args) {
        int array[] = {1,2,3,4};
        int[] copy = Arrays.copyOf(array,array.length);
        System.out.println(Arrays.toString(copy));
    }

    public static void main11(String[] args) {
        int array[] = {1,2,3,4};
        int[] copy = new int[array.length];
        System.arraycopy(array,0,copy,0,array.length);
        System.out.println(Arrays.toString(copy));
    }

    public static void main(String[] args) {
        int array[] = {1,2,3,4};
        int[] copy = array.clone();
        System.out.println(Arrays.toString(copy));
    }


    public static void main3(String[] args) {
        long i = 0;
        long sum = 0;
        while (i<9999999999L){
            i =i*10+9;
            sum += i;
        }
        System.out.println(sum);
    }

    public static void main4(String[] args) {
        int array[]= {1,2,3,4};
        int array2[]=new int[]{1,2,3,4};
        int array3[]=new int[10];
        array[0]=10;
        System.out.println(array[0]);
        System.out.println(array.length);
    }
    public static void main5(String[] args) {
        int array[] = {1, 2, 3, 4};
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] +" ");
        }
        System.out.println();//换行的作用
        for (int x:array) {
            //for-each 增强for 循环
            System.out.print(x + " ");
        }
        //借助Java本身的一些方法来实现数组的打印
        //Arrays.toString(array)   把参数的数组转化为字符串进行输出
        System.out.println();
        String ret = Arrays.toString(array);
        System.out.println(ret);
    }

    public static void main6(String[] args) {
        int array[] = null;
        System.out.println(array);
        /*  . 的前边为空  则异常
        System.out.println(array.length);*/
    }
//共用对象
    public static void main7(String[] args) {
        int[] array = {1, 2, 3, 4};
        int[] array1 =  array;
        System.out.println("array" + Arrays.toString(array));
        System.out.println("array1" + Arrays.toString(array1));
        System.out.println("---------------");
        array1[0]=99;
        System.out.println("array" + Arrays.toString(array));
        System.out.println("array1" + Arrays.toString(array1));
    }
    //虽然结果一样，但是地址不同
    public static void main8(String[] args) {
        int[] array1 = {1, 2, 3, 4};
        int[] array2 = {1, 2, 3, 4};
        System.out.println(array1);
        System.out.println(array2);
        //int[] array =null;   代表整个引用  不指向任何对象
    }

    /**
     * 形参与实参     掩耳盗铃
     * @param a
     */
//此处a为形参
    public static void fun(int a) {
        a = 20;
    }
    //此处x为实参
    public static void main9(String[] args) {
        int x = 10;
        fun(x);
        System.out.println(x);
    }
}
